package cn.com.yang.screen.v1;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
 * 屏幕截屏
 * TODO
 * @author yangyijin
 * @since 2023年3月21日-上午9:34:29
 */
public class CaptureScreen {
	public static void captureScreen(String fileName, String folder) throws Exception {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle screenRectangle = new Rectangle(screenSize);
		Robot robot = new Robot();
		BufferedImage image = robot.createScreenCapture(screenRectangle);
		// 保存路径
		File screenFile = new File(fileName);
		if (!screenFile.exists()) {
			screenFile.mkdir();
		}
		File f = new File(screenFile, folder);
		ImageIO.write(image, "png", f);
		// 自动打开
		if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
			Desktop.getDesktop().open(f);
	}

	public static void main(String[] args) {
		try {
			captureScreen("e:\\screen", "11.png");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
